package com.kungfuhacking.wristbandpro.find.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.net.ApiService;


/**
 * 客户服务
 * Created by Aaron on 17/5/10.
 */

public class CustomerServiceActivity extends BaseActivity {

    private WebView wv_about;
    private com.kungfuhacking.wristbandpro.custom.TitleBarView tbv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_webview);
        initWidget();

    }

    private void initWidget() {

        TitleBarView tbv = (TitleBarView) findViewById(R.id.tbv);
        tbv.setVisibility(View.GONE);
        wv_about = (WebView) findViewById(R.id.wv);
        MyWebViewClient client = new MyWebViewClient();

        WebSettings settings = wv_about.getSettings();

        settings.setDomStorageEnabled(true);
        settings.setJavaScriptEnabled(true);
        //动态加载js, JsInterface是我们定义的类,用于js调用, android 是动态加载到js代码里(html里面是 javascript:android.closeWindow();)
        //closeWindow 方法是js端调用的本地的方法
        wv_about.addJavascriptInterface(new JsInterface(), "android");

        wv_about.loadUrl(ApiService.QA);  // 加载网址
        wv_about.setWebViewClient(client);
        wv_about.setWebChromeClient(new WebChromeClient());

        wv_about.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });

    }

    @SuppressLint("SetJavaScriptEnabled")
    class JsInterface {

        public JsInterface() {
        }

        @JavascriptInterface
        public void closeWindow() {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            });

        }

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void onBackPressed() {

        if (wv_about.canGoBack())
            wv_about.goBack();
        else
            super.onBackPressed();

    }
}
